#ifndef _BSP_TKEY_H
#define _BSP_TKEY_H

#if USER_TKEY

#define CDPR            30
#define RISE_TIMES      2
#define PRESS_TIMES     4
#define FALL_TIMES      2
#define RELS_TIMES      4

enum
{
    NO_PRESS,           //无按下
    KSU_SHORT_PRESS,    //短按抬起
    KLU_LONG_PRESS,     //长按抬起
    KLLU_LLONG_PRESS,   //超长按/连按抬起
    KS_SHORT_PRESS,     //短按状态
    KL_LONG_PRESS,      //长按状态
    KLH_LONG_PRESS,     //持续长按
    KLL_LLONG_PRESS,    //超长按状态
};

#if TKEY_CH3
    #define TKEY_MAX_CH 3
#elif TKEY_CH2
    #define TKEY_MAX_CH 2
#elif TKEY_CH1
    #define TKEY_MAX_CH 1
#else //TKEY_CH0
    #define TKEY_MAX_CH 0 //默认通道
#endif // TKEY_CH3

#if TKEY_CH0
    #define TKEY_MIN_CH 0
#elif TKEY_CH1
    #define TKEY_MIN_CH 1
#elif TKEY_CH2
    #define TKEY_MIN_CH 2
#elif TKEY_CH3
    #define TKEY_MIN_CH 3
#else
    #define TKEY_MIN_CH 0
#endif // TKEY_CH0

typedef struct {
    u16 cdpr;
    u8 hw_en        : 1;    //硬件唤醒，只能打开1组
    u8 in_ear       : 1;    //入耳检测
    u8 res[1];
} tkey_ch_t;

typedef struct {
    //tk scan para
    tkey_ch_t *key[4];
    u8 rels_times;
    u8 fall_times;
    u8 press_times;
    u8 rise_times;
    u16 tkpthd;                 //touch key press threshold
    u16 tkrthd;                 //touch key release threshold
    u16 tkepthd;                //touch key in-ear threshold
    u16 tkerthd;                //touch key out-ear threshold
    u16 llong_press_times;      //超长按/连按时间设置(次数)
    u16 long_press_times;       //长按时间设置(次数)
    u16 short_press_times;      //短按时间设置(次数)
    u8 frq_long_press;          //长按返回灵敏度, 值越小, 相同时间内返回值越多,1为最灵敏,取值范围[1,255]
    u16 cail_tkcnt;             //校准计数延时,扫描按键时递减,减到5以下校准4个通道
    u16 cail_dwthd_left;        //低异常校准下限,tkcnt_val如果减小了,且减小的值在[cail_dwthd,cail_dwthd_right]范围内则被认为需要校准
    u16 cail_dwthd_right;       //低异常校准上限
    u16 cail_upthd_left;        //高异常校准下限,tkcnt_val如果增大了,且增加得值在[cail_upthd_left,cail_upthd_right]范围内则被认为需要校准
    u16 cail_upthd_right;       //高异常校准上限
    u16 cail_thd;               //ttimes_cali积累到该值则进行校准
    u8 res[1];
    //tk scan result
    u16 tkcnt_val[4];           //中断挂起后存储采集的TKCNT值
    u16 ttimes[4];              //扫描到的按下次数积累
    u16 ttimes_cali[4];         //需要校准的次数积累
} tkey_cfg_t;

void bsp_tkey_init(void);
u8 tkey_get_key(u8 ch);
u8 tkey_is_inear(void);     //入耳检测，0：无效，1：入耳，2：出耳
u8 tkey_wakeup_status(void);
void spp_tkcnt_tx(void);
u8 get_tkey_flag(void);
void bsp_tkey_scan(void);
void tkey_cail_set(u16 cnt,u16 dwthd_left,u16 dwthd_right,u16 upthd_left,u16 upthd_right,u16 thd);
#endif  //#if USER_TKEY

#endif
